/**
 * 
 */
package TextEditor;

import java.awt.AWTException;
import java.awt.Robot;
import java.awt.event.KeyEvent;
import java.util.ArrayList;
import java.util.Iterator;

/**
 * Class for performing macro recording and playing operations
 * @author Soen 6441 Group 4
 */
public class KeyboardMacroProcessor { //monitor?
	
	private final String HOTSTARTKEY="HOTSTARTKEY"; 
	private final String HOTSTOPKEY="HOTSTOPKEY"; 

	private ArrayList<KeyEvent> macrosteps = new ArrayList<KeyEvent>();
	private boolean stopFlag=false;	
	
	public KeyboardMacroProcessor(){
		this.macrosteps = new ArrayList<KeyEvent>();
	}
	
	public void playMacro(){		
		Iterator<KeyEvent> keys=macrosteps.iterator();				
        while(keys.hasNext()){
        	KeyEvent keyValue=keys.next();
        	try {
        			Robot robot = new Robot();
        			if(isHotkey(keyValue)){
        				robot.keyPress(keyValue.getKeyCode());
        			}
        			else{
        				robot.keyPress(keyValue.getKeyCode());
        				//robot.keyRelease(keyValue.getKeyCode());        				    				
        			}		        			         			

        		} catch (AWTException ex) {

        			ex.printStackTrace();

        		}  	          	
        }
		
	}
	private void doHotKeyProcess(KeyEvent keyValue) {
		// TODO Auto-generated method stub
		
	}

	private void doHotKeyProcess(KeyEvent keyValue, KeyEvent nextKey) {
		// TODO Auto-generated method stub		
		
	}

	private boolean isHotkey(KeyEvent keyValue) {
		// TODO Auto-generated method stub
		return (keyValue.isActionKey());
	}
	
	public void stopMacro(){		
		this.setStopFlag(true);	
		
	}
	public void recordMacro(KeyEvent e){	
				
		if(!this.getStopFlag()){
			macrosteps.add(e);
		}	
	}	
	public ArrayList<KeyEvent> getMacrosteps(){
		return macrosteps;		
	}
	private boolean getStopFlag() {
		// TODO Auto-generated method stub
		return this.stopFlag;
	}

	public void setStopFlag(boolean flag) {
		// TODO Auto-generated method stub
		this.stopFlag=flag;
	}

}
